package com.san.hkhospital.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.san.hkhospital.entity.MedUse;
import com.san.hkhospital.service.MedUseService;
import com.san.hkhospital.service.impl.MedUseServiceImpl;
import com.san.hkhospital.vo.ResultVO;
import com.san.hkhospital.vo.Search3;
import com.san.hkhospital.vo.meduseTableVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
* (med_use)表控制层
*
* @author xxxxx
*/
@RestController
@RequestMapping("/med_use")
public class MedUseController {
/**
* 服务对象
*/
@Resource
private MedUseServiceImpl medUseServiceImpl;
@Resource
private MedUseService medUseService;
/**
* 通过主键查询单条数据
*
* @param id 主键
* @return 单条数据
*/
@GetMapping(value = "/{id}")
public ResultVO selectOne(@PathVariable Integer id) {
    MedUse medUse =medUseService.find(id);
    if(medUse!=null){
        return ResultVO.success("一个用药单查找成功！",medUse);
    }else{
        return ResultVO.failed("一个用药单查找失败！");
    }
}
    @GetMapping
    public ResultVO medUseTableVOList(@RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "5") Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<meduseTableVO> medUseTableVOList = medUseService.findAll();
        PageInfo<meduseTableVO> pageInfo =new PageInfo<meduseTableVO>(medUseTableVOList);
        return ResultVO.success("所有用药单查询成功！",pageInfo);
    }
    @PostMapping
    public ResultVO add(@RequestBody MedUse medUse){
        boolean flag =  medUseService.add(medUse);
        if(flag){
            return ResultVO.success("添加用药单成功！");
        }else{
            return ResultVO.failed("添加用药单失败！");
        }
    }
    @PutMapping()
    public ResultVO update(@RequestBody MedUse medUse){
        boolean flag = medUseService.update(medUse);
        if(flag){
            return ResultVO.success("用药单修改成功！");
        }else{
            return ResultVO.failed("用药单修改失败！");
        }
    }
    @RequestMapping("Search")
    public ResultVO Search(@RequestBody Search3 search3, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<meduseTableVO> medUseTableVOList = medUseService.search(search3);
        PageInfo<meduseTableVO> pageInfo =new PageInfo<>(medUseTableVOList);
        return ResultVO.success("搜索查询成功！",pageInfo);
    }

}
